import requests
from lxml import etree

class IpSpider(object):
    def __init__(self):
        self.headers={
            "Accept-Encoding": "gzip, deflate,br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Connection": "keep-alive",
            "Upgrade-Insecure-Requests": "1",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36",
            "Host": "www.kuaidaili.com",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9"
        }

    def get_html(self):
        url="https://www.kuaidaili.com/free/"

        response=requests.get(url=url,headers=self.headers)

        if response:
            html=etree.HTML(response.text)
            iterms=html.xpath('//table[@class="table table-bordered table-striped"]/tbody//tr')
            return iterms

    def para_iterm(self,iterms):
        ip_list=[]
        for iterm in iterms:
            ip=iterm.xpath('./td[@data-title="IP"]/text()')
            ip_list.append(ip)
        return ip_list

    def preserve_ip(self,ip_list):
        list_m=[]
        sep = ','
        for i in range(0,15):
            for j in range(0,1):
                m=ip_list[i][j]
                list_m.append(m)
        with open('ip.txt','w')as f:
            f.write(sep.join(list_m))


def main():
    ip=IpSpider()
    iterm=ip.get_html()
    ip_list=ip.para_iterm(iterm)
    ip.preserve_ip(ip_list)


if __name__ == '__main__':
    main()
